package com.hongsheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.hongsheng.pojo.Bill;
import com.hongsheng.pojo.Package;
import com.hongsheng.pojo.R;
import com.hongsheng.service.PackgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @description 包装控制层
 **/
@RestController
@CrossOrigin
@RequestMapping("/package")
public class PackageController {

    //注入包装service
    @Autowired
    private PackgeService packgeService;

    /**
     * @description 包装列表分页查询，按包装类型查询
     **/
    @PostMapping ("{currentPage}/{size}")
    public R getBillPage(@PathVariable int currentPage,@PathVariable int size,@RequestBody Package pack){
        System.out.println(pack);

        IPage<Package> page = packgeService.getPage(currentPage,size,pack);
        //当前页大于总页数时出现bug
        if(currentPage>page.getPages()){
            //以总页数再次查询一次
            page=packgeService.getPage((int) page.getPages(), size,pack);

        }
        return new R(true,page);
    }

    /**
     * @description 修改包装
     * 包装信息以json传参
     **/
    @PutMapping()
    public R update(@RequestBody Package pack){//json接受参数
        return  new R(packgeService.updateById(pack));//根据包装id修改包装信息
    }

    /**
     * @description 添加包装
     * 包装信息以json传参
     **/
    @PostMapping()
    public R add(@RequestBody Package pack){
        return new R(packgeService.save(pack));
    }

    /**
     * @description 根据包装id查询包装信息
     **/
    @GetMapping("/{id}")
    public R selectBookById(@PathVariable Integer id){
        return new R(true,packgeService.getById(id));
    }

    /**
     * @description 删除包装
     **/
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Integer id){
        return new R(packgeService.removeById(id));
    }

}
